package com.lingyuan.cart.controller;

import com.lingyuan.cart.domain.CartEntity;
import com.lingyuan.cart.domain.CheckCart;
import com.lingyuan.cart.service.CartService;
import com.lingyuan.common.core.domain.R;
import com.lingyuan.common.core.exception.ServiceException;
import com.lingyuan.common.core.web.controller.BaseController;
import com.lingyuan.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName：CartController
 * @Author: LeiHao
 * @Date: 2024/9/18 10:08
 * @Description: 购物车控制层
 */
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController {
    @Autowired
    private CartService cartService;

    /* *
     * @Title: deleteCart
     * @Author: DELL
     * @Date: 2024-09-18 10:10:19
     * @Params: [skuId]
     * @Return: AjaxResult
     * @Description: 删除购物车记录
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable Long skuId) {
        if (skuId == null || skuId <= 0) {
            throw new ServiceException("请选择正确的商品");
        }
        cartService.deleteCart(skuId);
        return AjaxResult.success("删除成功");
    }
    /* *
     * @Title: checkCart
     * @Author: DELL
     * @Date: 2024-09-18 10:10:19
     * @Params: [skuIds]
     * @Return: AjaxResult
     * @Description: 修改购物车总价格
     */
    @GetMapping("/checkCart/{skuIds}")
    public AjaxResult checkCart(@PathVariable String skuIds) {
        if (skuIds == null || "".equals(skuIds)) {
            throw new ServiceException("请选择正确的商品");
        }
        CheckCart checkCart = cartService.checkCart(skuIds);
        return AjaxResult.success("修改成功成功",checkCart);
    }
    /* *
     * @Title: deleteBatchCart
     * @Author: DELL
     * @Date: 2024-09-18 10:10:19
     * @Params: [skuIds]
     * @Return: AjaxResult
     * @Description: 批量删除购物车记录
     */
    @DeleteMapping("/deleteBatchCart")
    public AjaxResult deleteBatchCart(@RequestBody List<Long> skuIds) {
        if (CollectionUtils.isEmpty(skuIds)) {
            throw new ServiceException("请选择正确的商品");
        }
        cartService.deleteBatchCart(skuIds);
        return AjaxResult.success("删除成功");
    }
    /* *
     * @Title: updateCart
     * @Author: DELL
     * @Date: 2024-09-18 10:10:19
     * @Params: [skuId, payNum]
     * @Return: AjaxResult
     * @Description: 修改成功
     */
    @PutMapping("/updateCart")
    public AjaxResult updateCart(Long skuId, Integer payNum) {
        if (skuId == null || skuId <= 0) {
            throw new ServiceException("请选择正确的商品");
        }
        if (payNum == null || payNum <= 0) {
            throw new ServiceException("请输入正确的购买数量");
        }
        cartService.updateCart(skuId, payNum);
        return AjaxResult.success("修改成功");
    }
    /* *
     * @Title: addCart
     * @Author: DELL
     * @Date: 2024-09-18 10:10:19
     * @Params: [skuIdAndNum]
     * @Return: AjaxResult
     * @Description: 添加购物车
     */
    @PostMapping("/addCart/{skuIdAndNum}")
    public AjaxResult addCart(@PathVariable("skuIdAndNum") String skuIdAndNum) {
        String[] split = skuIdAndNum.split(",");
        Long skuId = Long.valueOf(split[0]);
        if (skuId == null || skuId <= 0) {
            throw new ServiceException("请选择正确的商品");
        }
        Integer payNum = Integer.valueOf(split[1]);
        if (payNum == null || payNum <= 0) {
            throw new ServiceException("请输入正确的购买数量");
        }
        cartService.addCart(skuId, payNum);
        return AjaxResult.success("添加成功");
    }

    /* *
     * @Title: getCartEntityList
     * @Author: DELL
     * @Date: 2024-09-20 09:30:29
     * @Params: []
     * @Return: R<List<CartEntity>>
     * @Description: 查询当前登录用户购物车列表
     */
    @GetMapping("/getCartEntityList")
    public R<List<CartEntity>> getCartEntityList() {

        List<CartEntity> cartEntityList = cartService.getCartEntityList();
        return R.ok(cartEntityList);
    }

    /* *
     * @Title: findBySkuIdsCartEntityList
     * @Author: DELL
     * @Date: 2024-09-20 09:42:45
     * @Params: [skuIds]
     * @Return: R<List<CartEntity>>
     * @Description: 查询选中的购物车记录
     */
    @GetMapping("/findBySkuIdsCartEntityList/{skuIds}")
    public R<List<CartEntity>> findBySkuIdsCartEntityList(@PathVariable String skuIds) {

        List<CartEntity> cartEntityList = cartService.findBySkuIdsCartEntityList(skuIds);
        return R.ok(cartEntityList);
    }
}
